Implement hybrid retrieval with sparse keyword search and intelligent reranking #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a comprehensive hybrid retrieval system that combines dense semantic search with sparse keyword search, followed by intelligent reranking to improve retrieval accuracy and robustness.
Overview
The hybrid retrieval system addresses limitations of pure dense vector search by incorporating keyword-based matching and multi-signal reranking. This approach significantly improves retrieval performance, especially for queries requiring exact term matches or domain-specific terminology.
Key Features
🔍 Hybrid Search Function
📊 Intelligent Reranking Algorithm
The system combines multiple relevance signals with optimized weights:
🏗️ Dual Index Architecture
philosophy-rag- Existing managed embeddings for semantic searchphilosophy-rag-sparse- New TF-IDF weighted sparse vectors for keyword search🔧 Advanced Sparse Vector Construction
Technical Implementation
New Components
src/storage/sparse_store.py- Vocabulary management, TF-IDF calculation, sparse vector operationssrc/retrieval/hybrid_search.py- Result merging, reranking, and hybrid search orchestrationdata/vocab.json- Token-to-integer mapping for sparse vectorsdata/df.json- Document frequencies for IDF calculationsEnhanced Ingestion Pipeline
The ingestion process now creates both dense and sparse representations:
Graceful Degradation
Usage Examples
Basic Hybrid Search
# Test both semantic and hybrid search python src/scripts/test_search.pyDocument Ingestion with Hybrid Indexing
# Ingest documents creating both dense and sparse indexes python src/scripts/ingest_documents.pyPerformance Benefits
Backward Compatibility
All existing functionality remains unchanged:
This implementation provides a significant upgrade to retrieval quality while maintaining full backward compatibility with existing workflows.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
api.pinecone.iopython test_hybrid_retrieval.py(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.